package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CompanyDao;
import com.itheima.domain.Company;
import com.itheima.service.CompanyService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public PageInfo findByPage(int currentPage) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
        PageHelper.startPage(currentPage,5);
        List<Company> list = mapper.findAll();
        PageInfo info = new PageInfo(list);
        sqlSession.close();
        return info;
    }

    @Override
    public void addCompany(Company company) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(false);
        CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
        mapper.addCompany(company);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateCompany(Company company) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(false);
        CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
        mapper.updateCompany(company);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteCompany(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(false);
        CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
        mapper.deleteCompany(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Company> findAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
        List<Company> list = mapper.findAll();
        sqlSession.close();
        return list;
    }
}
